
module.exports = function handleInputTplCode(module_config, add_page) {

  var totalTplCode = ""; //输入组件容器
  var btnGroupTpl = ""; //按钮组
  var inputGroupStr = "";

  var inputDataContent = "";  //输入数据



  var formDataContent = `formData: {
    `;  //表单变量内容， 用于插入data
  var selectOptsContent = ``;

  for (var itemKey in add_page) {
    var item = add_page[itemKey];
    if (itemKey == 'formData') {
      for (var inputItemKey in item) {
        if (inputItemKey == 'titleText') 
          continue;
        if (inputItemKey.includes('richText')) {
          formDataContent += 'uploadApi' + `: "${item[inputItemKey].uploadApi}",
          `;
          continue;
        }
        if (inputItemKey.includes('date')) {
          var { dateConfig } = item[inputItemKey];
          formDataContent += `
              ${dateConfig.startDateName}: "",
              ${dateConfig.endDateName}: "",
            `;
          continue;
        }
        formDataContent += item[inputItemKey].inputName + `: "",
        `;
      }
    }
  }
  formDataContent += `},
  `;

  //输入框模板
  var inputGroupObj = getInputGroupStr(add_page);  //输入框组
  inputGroupStr = inputGroupObj.inputGroupStr;
  var { selectOptsContent } = inputGroupObj;

  //按钮模板
  btnGroupTpl = getBtnGroupTpl({
    addBtnText: add_page.addBtn.text, //添加按钮文本
    editBtnText: add_page.editBtn.text, //编辑按钮文本
    cancelText: add_page.cancelBtn.text, //取消文本
  });

  //总模板
  totalTplCode += getContainerTpl({
    inputGroupStr
  });
  totalTplCode += btnGroupTpl;


  //添加额外方法的data数据
  var extraDataStr = ``;
  for (var itemKey in add_page.extraRequest) {
    var extraItem = add_page.extraRequest[itemKey];
    if (itemKey == 'methodsCode') {
      extraItem.forEach((item, index) => {
        if (item.type == "opts") {
          extraDataStr += `
            ${item.dataName}: [],
          `;
        }
      });
    }
  }


  //输入数据data拼接
  inputDataContent += formDataContent;
  inputDataContent += selectOptsContent;
  inputDataContent += extraDataStr;

  return {
    totalTplCode,
    inputDataContent
  }

}



// 输入字段拼接
function getInputGroupStr(add_page) {
  var inputGroupStr = "";  //输入框组
  var selectOptsContent = ""; //选项数据

  for (var item in add_page) {
    var curItem = add_page[item];
    if (item == 'formData') {
      for (var inputItemKey in curItem) {
        var inputItemConfig = curItem[inputItemKey];

        if (inputItemKey.includes('input')) {
          var extraAttributeStr = '';
          if (inputItemConfig.disabledCondition) {
            extraAttributeStr += `:disabled= "${inputItemConfig.disabledCondition}" `;
          }
          var str = `<el-input 
            v-model="formData.${inputItemConfig.inputName}" 
            placeholder="请输入${inputItemConfig.labelName}" 
            size="small"
            style="width: 220px;"
            ${inputItemConfig.disabled ? ':disabled="true"' : ''}
            ${extraAttributeStr}
            ></el-input>`;
          inputGroupStr += getItemTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed
          });


        } else if (inputItemKey.includes('textarea')) {
          var { inputName, labelName, rows, width } = inputItemConfig;
          var str = `
            <el-input 
              type="textarea" 
              v-model="formData.${inputName}" 
              placeholder="请输入${labelName}" 
              size="small"
              :rows="${rows}"
              style="width: ${width}px; margin-left: -7px;"
              ></el-input>
          `;
          inputGroupStr += getItemTextareaTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed
          });


        } else if (inputItemKey.includes('select')) {
          var str = ` <el-select
            v-model="formData.${inputItemConfig.inputName}"
            style="width: 220px;"
            placeholder="请选择${inputItemConfig.labelName}"
            size="small"
          >
            <el-option
              v-for="(item, index) in ${inputItemConfig.selectOptsName}"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>`;

          //选项数据拼接
          selectOptsContent += `${inputItemConfig.selectOptsName} : ${inputItemConfig.selectOptsArr} `;

          inputGroupStr += getItemTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed
          });

        } else if (inputItemKey.includes('uploadImg')) {
          var { countLimit, labelName, inputName, uploadMethod, showCondition } = inputItemConfig;
          var str = `
            <upload-img 
              labelName= "${labelName}" 
              :imgArrProp="formData.${inputName}"
              :countLimit="${countLimit}"
              @uploadSuccess="${uploadMethod}"
            />
          `;
          inputGroupStr += getItemImgTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed,
            showCondition: showCondition,
          });
        } else if (inputItemKey.includes('radio')) {
          var radioStr = "";
          var radioWidth = inputItemConfig.radioWidth ? inputItemConfig.radioWidth : "60px";
          inputItemConfig.radioOpts.forEach((item, index) => {
            var str = `
            <div class="radio-box">
              <el-radio style="width: ${radioWidth};" v-model="formData.${inputItemConfig.inputName}" :label="'${item.value}'">${item.label}</el-radio>
            </div>
            `;
            radioStr += str;
          })

          inputGroupStr += getItemTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: radioStr,
            isNeed: inputItemConfig.isNeed
          });

        } else if (inputItemKey.includes('titleText')) {
          var str = `
            <div class="title-text">${inputItemConfig}</div>
          `;
          inputGroupStr += str;

          
        } else if (inputItemKey.includes('richText')) {
          var { labelName, inputName, minHeight,
            editContentChangeMethodName, showCondition } = inputItemConfig;
            /* 
                 <bg-editor
              :minHeight="${minHeight}"
              :uploadUrl="formData.uploadApi"
              :content="formData.${inputName}"
              @editorData="${editContentChangeMethodName}"
            ></bg-editor>
            
            */
          var str = `
            <richTextAssembly
              @changeSuccess="${editContentChangeMethodName}"
              :value="formData.${inputName}"
            ></richTextAssembly>
          `;
          inputGroupStr += getRichTextBoxTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed,
            minHeight: Number(inputItemConfig.minHeight) + 20,
            showCondition: showCondition,
          });
          
        } else if (inputItemKey.includes('date')) {
          var { labelName, inputName, minHeight, dateConfig } = inputItemConfig;
          //日期
          var str = `
            <date-edit
              :tThis="this"
              :startDateDefault="formData.${dateConfig.startDateName}"
              :endDateDefault="formData.${dateConfig.endDateName}"
              labelWidth="80px;"
              dateWidth="150px;"
              @dateChange="${dateConfig.dateChangeMethodsName}"
            /> 
          `;
          inputGroupStr += getItemTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed,
          });
        } else if (inputItemKey.includes('chooseSimgleTime')){
          var { labelName, inputName, minWidth } = inputItemConfig;
          var str = `
          <el-date-picker
            v-model="formData.${inputName}"
            type="datetime"
            width="${minWidth || ''}"
            placeholder="${labelName}">
          </el-date-picker>
          `;
          inputGroupStr += getItemTpl({
            labelName: inputItemConfig.labelName,
            inputConStr: str,
            isNeed: inputItemConfig.isNeed,
          });
        }

        // else if (inputItemKey.includes('fileUpload')) {
        //   var str = `
        //   <el-upload
        //     class="upload-demo"
        //     :action="javascript:;"
        //     :on-preview="handlePreview"
        //     :on-remove="handleRemove"
        //     :before-remove="beforeRemove"
        //     multiple
        //     :limit="3"
        //     :on-exceed="handleExceed"
        //     :file-list="fileList">
        //     <el-button size="small" type="primary">点击上传</el-button>
        //     <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        //   </el-upload>
        //   `;
        //   var str1 = "";
        //   inputGroupStr += getItemImgTpl({
        //     labelName: inputItemConfig.labelName,
        //     inputConStr: str1,
        //     isNeed: inputItemConfig.isNeed
        //   });
        // }
        //

      }
    }
  }

  return {
    inputGroupStr,
    selectOptsContent, //选项菜单
  };
}

//输入组外层盒子
function getContainerTpl(opt) {
  var inputGroupStr = opt.inputGroupStr;
  return `
  <div class="edit-wrap">
    <div class="top-input">
      ${inputGroupStr}
    </div>
  </div>
  `;
}

//输入项盒子
function getItemTpl(opt) {
  var labelName = opt.labelName,
    inputConStr = opt.inputConStr,
    isNeed = opt.isNeed == 'true' ? true : false;
  return `
  <div class="item">
    <label for>
      <span class="tips">${isNeed ? '*' : ''} </span>${labelName}：
    </label>
    <div class="inline-block">
      ${inputConStr}
    </div>
  </div>
  `;
}

//输入项 图片上传盒子
function getItemTextareaTpl(opt) {
  var labelName = opt.labelName,
    inputConStr = opt.inputConStr,
    isNeed = opt.isNeed == 'true' ? true : false;
  return `
  <div class="item img-item">
    <label for>
      <span class="tips">${isNeed ? '*' : ''} </span>${labelName}：
    </label>
    <div class="inline-block" style="padding-left: 12px;">
      ${inputConStr}
    </div>
  </div>
  `;
}

//富文本组件
function getRichTextBoxTpl(opt) {
  var labelName = opt.labelName,
    inputConStr = opt.inputConStr,
    isNeed = opt.isNeed == 'true' ? true : false,
    minHeight = opt.minHeight ? opt.minHeight : "",
    showCondition = opt.showCondition || "";

  return `
  <div class="item rich-text-item" 
    style="height: ${Number(minHeight) + 30}px; width: 920px;"
    ${showCondition ? 'v-show="' + showCondition + '"' : ''}
    >
    <label for>
      <span class="tips">${isNeed ? '*' : ''} </span>${labelName}：
    </label>
    <div class="inline-block" style="padding-left: 10px;">
      ${inputConStr}
    </div>
  </div>
  `;
}

//输入项 图片上传盒子
function getItemImgTpl(opt) {
  var labelName = opt.labelName,
    inputConStr = opt.inputConStr,
    isNeed = opt.isNeed == 'true' ? true : false,
    showCondition = opt.showCondition || "";
  return `
  <div class="item img-item"
    ${showCondition ? 'v-show="' + showCondition + '"' : ''}
    >
    <label for>
      <span class="tips">${isNeed ? '*' : ''} </span>${labelName}：
    </label>
    <div class="inline-block">
      ${inputConStr}
    </div>
  </div>
  `;
}

//按钮
function getBtnGroupTpl(opt) {
  var addBtnText = opt.addBtnText ? opt.addBtnText : '添加',
    editBtnText = opt.editBtnText ? opt.editBtnText : '编辑',
    cancelText = opt.cancelText ? opt.cancelText : '返回';
  return `
  <div class="submit-option">
    <el-button v-if="!isEdit" @click="handleAdd" size="small" type="primary">${addBtnText}</el-button>
    <el-button v-else @click="handleEdit" size="small" type="primary">${editBtnText}</el-button>
    <el-button @click="handleCancel" size="small" type="primary">${cancelText}</el-button>
  </div>
  `
}
